home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5461 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.5 KB  |  93 lines

  1. Path: news.primenet.com!not-for-mail
  2. From: gbe@primenet.com (Gary Edstrom)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: What is Object Oriented Programming (OOP)?
  5. Date: 4 Feb 1996 08:19:01 -0700
  6. Organization: Sequoia Software
  7. Sender: root@primenet.com
  8. Message-ID: <3114c73f.290832739@news.primenet.com>
  9. References: <4f1nj6$4cie@news-s01.ny.us.ibm.net>
  10. X-Posted-By: ip037.lax.primenet.com
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. >sansw@ibm.net (SAN'Z) wrote:
  14.  
  15. >Hello to all internet user,
  16.  
  17. >I'm just starting to learn C++ and I want to know about OOP.  
  18. >Can you explain to me about that? And I really want to know and study it.
  19.  
  20. A good real world analogy of an object would be your car.  A car consists of a
  21. number of input functions such as the accelerator, brake, ignition key, etc.
  22. It also consists of a number of output functions such as the dashboard display
  23. of speed, distance, engine temperature, fuel, etc.  You, as the user of this
  24. object, do not need to really have much knowledge of how it handles these
  25. functions internally, you just know that to make the car go faster, you press
  26. on the accelerator.
  27.  
  28. Objects in the programming are similar.  In a C++ object, we hide the details
  29. about how data is stored inside the object, as well as exactly how the various
  30. functions are performed internally.  An object consists of both data, and a
  31. series of functions that can manipulate that data.  Normally, you only make the
  32. functions visible to the outside world and hide the data.
  33.  
  34. One common C++ data type is the "complex" type.  The knowledge of how to
  35. manipulate complex numbers is not something that has been hard coded into the
  36. compiler.  Instead, an object called "complex" has been defined that performs
  37. all of the functions that we commonly associate with complex numbers, such as
  38. addition, subtraction, multiplication, etc.  To the programmer, using a complex
  39. number is a simple process:
  40.  
  41.     complex x(3,7);
  42.     complex y(4,5);
  43.     complex z = x * y;
  44.  
  45. Hidden behind the scenes, however, exist header and code files that implement
  46. the "complex" data type.  A simplified definition of the "complex" type that
  47. would handle the above sample would be as follows:
  48.  
  49.   class complex
  50.     {
  51.     private:
  52.       float real;
  53.       float imag;
  54.  
  55.     public:
  56.       complex() { }
  57.       complex(float a, float b) { real = a; imag = b; }
  58.  
  59.       const complex & operator = (const complex & source) 
  60.         { 
  61.         real = source.real; 
  62.         imag = source.imag; 
  63.         }
  64.  
  65.       complex operator * (const complex & source)
  66.         {
  67.         complex temp = *this;
  68.         temp.real = real * source.real - imag * source.imag;
  69.         temp.imag = real * source.imag + imag * source.real;
  70.         return temp;
  71.         }
  72.     };
  73.  
  74. Of course, the real "complex" class implements a lot more functions than I have
  75. shown here.  All of the familiar C++ operators such as +=, -=, *=, etc need to
  76. be defined if we are going to create a class that has all of the operations
  77. that we normally associate with simple integers.
  78.  
  79. Not all user defined data types would necessarily have to overload all of the
  80. operators.  For a particular data type, some of the operators may simply make
  81. no sense at all.  Therefore, they are left out.
  82.  
  83. Once again: The purpose of an object is to group a number of related items
  84. together and to hide the details of exactly how those items are manipulated.
  85.  
  86. I hope that helps a little.
  87.  
  88. --
  89. Gary Edstrom <gbe@primenet.com> | Sequoia Software
  90. PO Box 9573                     | Programming & Technical Services
  91. Glendale CA 91226-0573          | PGP Key ID: 0x1A0D44BD
  92. PGP Fingerprint: 72 AA 4F 73 05 53 89 C6  8A EE F4 EE D1 C0 13 8D 
  93.